import heapq
import collections


class Solution(object):
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        c = collections.Counter(nums)
        c_list = [(c[key], key) for key in c]
        heap = c_list[: k]
        heapq.heapify(heap)
        for i in range(k, len(c_list)):
            if c_list[i][0] > heap[0][0]:
                heapq.heapreplace(heap, c_list[i])
        return [item[1] for item in heap]


data = Solution()
nums = [1]
k = 1
print(data.topKFrequent(nums, k))
